<?php

/**
 * Acl array adapter
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Array.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Acl_Array
{
    /**
     *
     * @param string $username
     * @return array User details
     */
    public function getUserByUserName($username)
    {
        $users = new Admin_Array_Users();
        return $users->getUserByUsername($username);
    }

    /**
     *
     * @param int $user
     * @return array Array of role details
     */
    public function getUserRoles($user)
    {
        $userRoles = new Admin_Array_UserRoles();
        $roles = $userRoles->getRolesByUserId($user['id']); // returns an array of role ids

        // explode roleIds into full blown roles
        $roleList = new Admin_Array_Roles();
        foreach ($roles as &$roleId) {
            $roleId = $roleList->getRoleById($roleId);
        }

        return $roles;
    }

    /**
     *
     * @param mixed $roleId
     * @return array Role details
     */
    public function getRoleById($roleId)
    {
        $roles = new Admin_Array_Roles();
        return $roles->getRoleById($roleId);
    }

    /**
     *
     * @param mixed $role
     * @return array Array of role details
     */
    public function getParentRoles($role)
    {
        $roles = new Admin_Array_Roles();
        return $roles->getParentRoles($role['id']);
    }

    public function getAllRoles()
    {
        $roles = new Admin_Array_Roles();
        return $roles->getAllRoles();
    }

    /**
     *
     * @param array $res Array containing res id and priv
     * @return array Array of role ids
     */
    public function getRulesByResource($res)
    {
        $permissions = new Admin_Array_Permissions();
        return $permissions->getRulesByResource($res);
    }

    /**
     *
     * @return array Array of permission details
     */
    public function getAllPermissions()
    {
        $permissions = new Admin_Array_Permissions();
        return $permissions->getAll();
    }

    protected $_resourceNameSpec = ':module/:controller';
    protected $_resourcePrivSpec = ':action';

    /**
     * Generate resource id and priv
     *
     * $params must contain the keys 'module', 'controller' and 'action'
     *
     * @param array $params Uses the current request if not passed
     * @return array containing resourceId and privilege
     */
    public function generateResource(array $params = null)
    {
        if (null !== $params) {
            $module = $params['module'];
            $controller = $params['controller'];
            $action = $params['action'];
        } else {
            $req = Zend_Controller_Front::getInstance()->getRequest();
            $module = $req->getModuleName();
            $controller = $req->getControllerName();
            $action = $req->getActionName();
        }

        $replaceMap = array(
            ':module' => $module,
            ':controller' => $controller,
            ':action' => $action,
        );

        $resId = str_replace(
            array_keys($replaceMap),
            array_values($replaceMap),
            $this->_resourceNameSpec
        );

        $priv = str_replace(
            array_keys($replaceMap),
            array_values($replaceMap),
            $this->_resourcePrivSpec
        );

        return array('id' => $resId, 'privilege' => $priv);
    }

    public function getResource($params = null)
    {
        return $this->generateResource($params);
    }

}
